package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.callback.OnTabSelectedListener;
import com.sgb.kjwl.databinding.ActivityOwnerAndDriverInquiryBinding;
import com.sgb.kjwl.view.constant.RouterKey;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LogisticsEvent;
import com.swgk.core.event.MenuEvent;
import com.sgb.kjwl.view.ui.fragment.transport.OwnerAndDriverIFragment;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * @Author : 熊丽
 * @date :2021/4/9 0009$ 10:12$
 * @Description :报名货主和司机询价管理列表界面$$
 */
public class OwnerAndDriverInquiryActivity extends AppActivity {
    private ActivityOwnerAndDriverInquiryBinding mBinding;
    private boolean isOwnerList;
    private String[] driverTitles = {"全部","待确认","已同意", "超时未确认"};
    private String[]  ownerTitles= {"全部","询价待确认","同意询价", "超时未确认"};
    private ArrayList<Fragment> mFragments=new ArrayList<>();
    @Override
    protected void init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        isOwnerList = getIntent().getExtras().getBoolean(RouterKey.BOOLEAN);
         mBinding = DataBindingUtil.setContentView(this, R.layout.activity_owner_and_driver_inquiry);
          mBinding.setLifecycleOwner(this);
         mBinding.includeTitle.titleView.setText(isOwnerList?"我的询价":"货主询价");
        initFragment();
        initEvent();
    }
    public static void start(Context context,boolean isOwnerList) {
        Intent intent = new Intent(context, OwnerAndDriverInquiryActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(RouterKey.BOOLEAN, isOwnerList);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }
    private void initEvent() {
         mBinding.includeTitle.ivBack.setOnClickListener(this);
    }

    private void initFragment() {
        //添加页面
        for (int i = 0; i < (isOwnerList?ownerTitles.length:driverTitles.length); i++) {
            mFragments.add(OwnerAndDriverIFragment.newInstance(i,isOwnerList));
        }
        SlidingFragmentPagerAdapter pagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, isOwnerList?ownerTitles:driverTitles);
         mBinding.vpOwnerAndDriver.setOffscreenPageLimit(isOwnerList?ownerTitles.length:driverTitles.length);
         mBinding.vpOwnerAndDriver.setAdapter(pagerAdapter);
         mBinding.tabLayout.setupWithViewPager(mBinding.vpOwnerAndDriver);
         mBinding.tabLayout.addOnTabSelectedListener(new OnTabSelectedListener() {
             @Override
             public void onTabSelected(TabLayout.Tab tab) {
                 refreshData();
             }
         });
        mBinding.vpOwnerAndDriver.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refreshData();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LogisticsEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_REFRESH_OWNER_INQUIRY) {
            refreshData();
        }
    }
    private void refreshData() {
        ((OwnerAndDriverIFragment) mFragments.get(mBinding.tabLayout.getSelectedTabPosition())).refreshData(mBinding.tabLayout.getSelectedTabPosition());

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MenuEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_ORDER_FINISH) {
            refreshData();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
